package cn.butcher21.controller;

import cn.butcher21.dao.DaoWork;
import cn.butcher21.dao.DaoWorkImpl.DaoWorkImpl;
import cn.butcher21.pojo.GroupTask;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "getAllGroupTaskMsgServlet", value = "/getGroupTaskMsg")
public class getAllGroupTaskMsgServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String date = request.getParameter("date");
        if (date == null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            date = sdf.format(new Date());
        }

        DaoWork daoWork = new DaoWorkImpl();
        try {
            List<GroupTask> groupTasks = daoWork.getGroupTasks(date);
            Gson gson = new Gson();
            String json = gson.toJson(groupTasks);
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.print(json);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
